package com.bootdo.app.common.config;

import com.bootdo.app.common.Interceptor.TokenInterceptor;
import com.bootdo.app.common.xss.XSSFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
  * @auther: Along
  * @description: web 配置类
  */
@Configuration
public class WebConfig implements WebMvcConfigurer{

    /**
     * 不拦截路径
     * @return
     */
    private List<String> excludePath(){
        List<String> list = new ArrayList<>();
        String[] urls = new String[]{

        };
        Collections.addAll(list,urls);
        return list;
    }
    /**
     * 默认添加JWT-token拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("-----添加拦截器。。。");
        registry.addInterceptor(getTokenInterceptor()).addPathPatterns("/app/**").excludePathPatterns(excludePath());
    }
    @Bean
    public TokenInterceptor getTokenInterceptor(){
        return new TokenInterceptor();
    }

    /**
     * 添加xssFilter
     */
//    @Bean
//    public Filter getFilter(){
//        return new XSSFilter();
//    }
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean(){
//        System.out.println("-----添加XSS过滤器。。。");
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//        filterRegistrationBean.setFilter(getFilter());
//        filterRegistrationBean.addUrlPatterns("/app/**");
//        filterRegistrationBean.setName("xssFilter");
//        filterRegistrationBean.setOrder(0);
//        return filterRegistrationBean;
//    }

    /**
     * 响应json数据序列化设置
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 创建jack消息转换对象
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        //不显示为null的字段
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //放到第一个
        converters.add(0, jackson2HttpMessageConverter);
    }


}
